package com.squeekeek.util.qrcode.Ui;

import com.squeekeek.util.qrcode.R;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class InfoFragment extends Fragment {
	public static final String FIELD_ECMODE = "errorcorrectmode";
	public static final String FIELD_MODSIZE = "modulesize";
	public static final String FIELD_VERSION = "version";
	public static final String FIELD_FILEPATH = "filepath";
	public static final String FIELD_DATASTREAM = "datastream";
	public static final String FIELD_ECSTREAM = "errorcorrectstream";
	
	private OnButtonClick hListener;
	
	public InfoFragment() {
		this.getArguments();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.activity_info, container, false);	
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		
		try {
			hListener = (OnButtonClick) activity;
		}
		catch(ClassCastException e) {
			Log.d("QRCode", "Cannot cast: " + e.getMessage());
		}
	}
	
	@Override
	public void onResume() {
		String textInfo;
		Button cancelBtn;
		TextView textPane;
		ImageView imagePane;
		Bitmap imageMap;
		Bundle args;
		
		super.onResume();
		
		cancelBtn = (Button) getActivity().findViewById(R.id.btn_new);
		textPane = (TextView) getActivity().findViewById(R.id.text_info);
		imagePane = (ImageView) getActivity().findViewById(R.id.img_processed);
		
		// Set image pane
		args = this.getArguments();
		imageMap = BitmapFactory.decodeFile(args.getString(FIELD_FILEPATH));
		imagePane.setImageBitmap(imageMap);
		
		// Set text info
		textInfo = "";
		textInfo += "Version: " + args.getInt(FIELD_VERSION) + "\n";
		textInfo += "Metadata: " + args.getString(FIELD_ECMODE) + "\n";
		textInfo += "Module Width: " + String.format("%.2f", args.getDouble(FIELD_MODSIZE)) + "\n";
		textInfo += "Raw Data:\n" + args.getString(FIELD_DATASTREAM);
		textPane.setText(textInfo);
		textPane.setMovementMethod(new ScrollingMovementMethod());
		
		// Add button listener
		cancelBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				onCancelButton(v);
			}
			
		});
	}
	
	public void onCancelButton(View v) {
		hListener.onCancelButtonClick();
	}
	
	public interface OnButtonClick {
		public void onCancelButtonClick();
	}
}
